/**
 * 外面页面
 */
import { getshoplist, getGoodsById, getComments } from '@/api/ajax'
import { formatDate } from '@/utils/tools'
const OSSURL_BASE = `http://admi-yuliner.oss-cn-hangzhou.aliyuncs.com/`
export default {
   namespaced: true,
   state() {
      return {
         shoplist: JSON.parse(sessionStorage.getItem('shoplist') || '[]'), //商品列表数据
         shop: JSON.parse(sessionStorage.getItem('shop') || '{}'), // 店铺信息数据
         goods: JSON.parse(sessionStorage.getItem('goods') || '[]'), // 店铺对应的所有商品
         carts: [], // 购物车数据
         comments: [], // 店铺的评价数据
      }
   },
   actions: {
      /* 获取商品列表 */
      async getShopList(context) {
         let data = await getshoplist()
         if (!data) return
         let newdata = data.map(item => {
            item.logo = OSSURL_BASE + 'shoplist/' + item.logo
            return item
         })
         context.commit('SAVE_SHOPLIST', newdata)
      },
      /* 根据ID值获取对应店铺的所有商品 */
      async getGoodsById(context, id) {
         let { goods, msg } = await getGoodsById(id)
         if (msg !== 'ok') return
         goods = goods.map(item => {
            item.foodpic = OSSURL_BASE + 'shopgoods/' + item.foodpic
            item.count = 0 // 初始化商品数量
            return item
         })
         context.commit('SAVE_GOODS', goods)
      },
      /* 根据ID获取店铺评论数据 */
      async getShopComments(context, shopId) {
         let { comments, msg } = await getComments(shopId)
         if (msg !== 'ok') return
         if (!comments) return
         comments = comments.map(item => {
            item.userpic = OSSURL_BASE + 'avatar/' + item.userpic
            item.comments_time = formatDate(item.comments_time)
            return item
         })
         context.commit('SAVE_SHOP_COMMENTS', comments)
      }
   },
   mutations: {
      /* 存储外卖页的商品列表 */
      SAVE_SHOPLIST(state, val) {
         if (!val) return
         state.shoplist = val
         sessionStorage.setItem('shoplist', JSON.stringify(val))
      },
      /* 根据ID存储应的店铺信息数据 */
      SAVE_BY_ID(state, id) {
         const shop = state.shoplist.find(item => item.id === Number(id))
         if (!shop) return
         state.shop = shop
         sessionStorage.setItem('shop', JSON.stringify(shop))
      },
      /* 存储店铺的所有商品 */
      SAVE_GOODS(state, goods) {
         state.goods = goods
         sessionStorage.setItem('goods', JSON.stringify(goods))
      },
      /**
       * 更新指定ID商品的数量
       * @param {Boolean} type 是否执行加/减操作
       * @param {Number} foodId 商品ID
       */
      UPDATE_GOODS_COUNT(state, {type, foodId}) {
         let food = state.goods.find(item => item.id == Number(foodId))
         // 减到最小值为0时取消操作
         if (!type && food.count <= 0) return
         // 添加一条商品到购物车
         if (type && food.count == 0) state.carts.push(food)
         // 改变商品数量
         type ? food.count++ : food.count--
         // 数量等于0时将商品从购物车中删除
         if (food.count <= 0 && !type) state.carts.splice(state.carts.indexOf(food), 1)
         // 更新session中的数据
         sessionStorage.setItem('goods', JSON.stringify(state.goods))
      },
      /* 清空购物车 */
      CLEAR_CARTS(state) {
         state.goods.forEach(item => {item.count = 0})
         state.carts = []
      },
      /* 存储店铺评论信息 */
      SAVE_SHOP_COMMENTS(state, comments) {
         state.comments = comments
      },
      /* 清空店铺评论 */
      CLEAR_COMMENTS(state) {
         state.comments = []
      }
   },
   getters: {
      /* 计算出购物车的商品总价，并返回数据 */
      getTotalMoney(state) {
         return state.carts.reduce((prevMoney, food) => prevMoney += Number(food.price) * Number(food.count), 0)
      }
   }
}